﻿using Microsoft.AspNetCore.Mvc.Filters;
using System.Diagnostics;

namespace Infrastructure.Filter
{
    /// <summary>
    /// 返回结果过滤器，统一返回数据格式
    /// </summary>
    public class AppResultFilter : Attribute, IActionFilter
    {
        /// <summary>
        /// 请求时长计时开始
        /// </summary>
        private readonly Stopwatch watch = new Stopwatch();

        public void OnActionExecuted(ActionExecutedContext context)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 返回结果执行之前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            watch.Start();//开始  
        }

        /// <summary>
        /// 返回结果执行之后
        /// </summary>
        /// <param name="context"></param>
        //public void OnActionExecuted(ActionExecutedContext context)
        //{

        //    watch.Stop();
        //    //根据实际需求进行具体实现
        //    if (context.Result is ObjectResult)
        //    {
        //        var objectResult = context.Result as ObjectResult;
        //        if (objectResult.Value == null)
        //        {
        //            context.Result = new ObjectResult(new ApiResult { Code = HttpStatusCode.NotFound, Msg = "defeated", TimeOut = watch.ElapsedMilliseconds });
        //        }
        //        else
        //        {
        //            if (objectResult.Value is ApiResult)
        //            {
        //                var result = (ApiResult)objectResult.Value;
        //                result.TimeOut = watch.ElapsedMilliseconds;
        //                context.Result = new ObjectResult(result);
        //                return;
        //            }
        //            context.Result = new ObjectResult(new ApiResult { Code = HttpStatusCode.OK, Msg = "success", Data = objectResult.Value, TimeOut = watch.ElapsedMilliseconds });
        //            //判读是否返回的是元组
        //            //返回数据并且返回总行数 public async Task<（List<object>,int）> Paging(PagingBaseRequest parameter) { retuen ( new List<object>{ },1000) }
        //            if (objectResult.DeclaredType != null && objectResult.DeclaredType.Name == "ValueTuple`2")
        //            {
        //                dynamic value = objectResult.Value;
        //                if (value.Item1 != null)
        //                {
        //                    if (value.Item1 is int)
        //                    {
        //                        //返回元组格式（int,List<object>）
        //                        context.Result = new ObjectResult(new ApiResult { Code = HttpStatusCode.OK, Msg = "success", TimeOut = watch.ElapsedMilliseconds, Data = new { Count = value.Item1, Data = value.Item2 } });
        //                    }
        //                    else
        //                        //返回元组格式（List<object>,int）
        //                        context.Result = new ObjectResult(new ApiResult { Code = HttpStatusCode.OK, Msg = "success", TimeOut = watch.ElapsedMilliseconds, Data = new { Count = value.Item2, Data = value.Item1 } });
        //                }
        //            }

        //        }
        //    }
        //    else if (context.Result is EmptyResult)
        //    {
        //        context.Result = new ObjectResult(new ApiResult { Code = HttpStatusCode.OK, Msg = "success" });
        //    }
        //    else if (context.Result is ContentResult)
        //    {
        //        context.Result = new ObjectResult(new ApiResult { Code = HttpStatusCode.OK, Msg = "", TimeOut = watch.ElapsedMilliseconds, Data = (context.Result as ContentResult).Content });
        //    }
        //    else if (context.Result is StatusCodeResult)
        //    {
        //        context.Result = new ObjectResult(new { HttpStatus = (context.Result as StatusCodeResult).StatusCode, TimeOut = watch.ElapsedMilliseconds, Msg = "" });
        //    }
        //    else if (context.Result is Exception)
        //    {
        //        var result = context.Result as Exception;
        //        context.Result = new ObjectResult(new { HttpStatus = HttpStatusCode.BadRequest, TimeOut = watch.ElapsedMilliseconds, Msg = result.Msg });
        //    }
        //}

    }
}
